package com.smartfast4j.manager.modules.file.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartfast4j.common.Constants;
import com.smartfast4j.common.annotation.LogRecord;
import com.smartfast4j.common.bean.R;
import com.smartfast4j.common.util.ConversionUtil;
import com.smartfast4j.manager.common.controller.ManagerBaseController;
import com.smartfast4j.manager.modules.file.service.TestFileManagerService;
import com.smartfast4j.manager.modules.sys.service.SysUserService;
import com.smartfast4j.pojo.file.TestFileManager;
import com.smartfast4j.pojo.file.TestFileManagerExample;
import com.smartfast4j.pojo.sys.SysUser;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
* TestFileManagerController
*  由代码生成器生成
*/
@RestController
@RequestMapping("/testFileManager")
public class TestFileManagerController extends ManagerBaseController {

    @Autowired
    private TestFileManagerService testFileManagerService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 保存上传文件
     * @param uploadFiles
     * @return
     */
    @LogRecord(operation = "上传测速文件",remark = "上传测速文件",moduleName = "测速管理")
    @RequestMapping("/saveUploadFile")
    public R saveUploadFile(@RequestBody List<TestFileManager> uploadFiles){
        if(CollectionUtils.isNotEmpty(uploadFiles)){
            for(TestFileManager fileManager:uploadFiles){
                fileManager.setCreateTime(new Date());
                fileManager.setCreateUser(getLoginUserId());
                fileManager.setUpdateTime(new Date());
                fileManager.setUpdateUser(getLoginUserId());
            }
            testFileManagerService.insertBatchSelective(uploadFiles);
            return R.ok();
        }else {
            return R.fail("文件上传失败!");
        }
    }

    /**
     * 获取测速文件列表
     * @return
     */
    @RequestMapping("/list")
    public R list(Integer offset, Integer limit,String fileName) {
        if (offset != null && limit != null) {
            //分页查询
            PageHelper.offsetPage(offset, limit);
        }
        TestFileManagerExample example = new TestFileManagerExample();
        TestFileManagerExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteFlagEqualTo(Constants.DELETE_FLAG.NO);//未删除
        //只有超级管理员，才能查看所有数据列表
        if (!getLoginUserRoleIds().contains(Constants.SUPER_ADMIN)) {
            criteria.andCreateUserEqualTo(getLoginUserId());
        }
        if(StringUtils.isNotEmpty(fileName)){
            criteria.andOriginalFileNameLike("%"+fileName+"%");
            example.or().andNewFileNameLike("%"+fileName+"%");
        }
        example.setOrderByClause("create_time desc");
        //只有紧跟在 PageHelper.startPage 方法后的第一个 MyBatis 的查询(select)方法会被分页。
        List<TestFileManager> testFileManagerList = testFileManagerService.selectByExample(example);
        PageInfo<TestFileManager> pageInfo = new PageInfo<>(testFileManagerList);

        List<Map<String,Object>> resultList = new ArrayList<>();

        for (TestFileManager testFileManager : testFileManagerList){
            //bean转成Map
            Map<String,Object> map = ConversionUtil.beanToMap(testFileManager);
            SysUser updateUser = null;
            if(testFileManager.getUpdateUser()!=null){
                updateUser = sysUserService.getSysUser(testFileManager.getUpdateUser());
            }
            SysUser createUser = null;
            if(testFileManager.getCreateUser()!=null){
                createUser = sysUserService.getSysUser(testFileManager.getCreateUser());
            }

            if(ConversionUtil.isNotEmptyParameter(updateUser)){
                //添加额外字段返回
                map.put("createUser",createUser.getRealName());//创建人
                map.put("updateUser",updateUser.getRealName());//更新人
            }
            resultList.add(map);
        }

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("total",pageInfo.getTotal());
        dataMap.put("rows", resultList);
        //服务端分页，返回的结果必须包含total、rows两个参数。漏写或错写都会导致表格无法显示数据。
        return R.ok(dataMap);
    }

    /**
     * 根据资源id批量删除资源
     * @param testFileIdList 要删除的文件
     * @return
     */
    @LogRecord(operation = "删除测速文件",remark = "根据id删除测速文件",moduleName = "测速管理")
    @RequestMapping(value = "/delete")
    public R delete(@RequestBody List<String> testFileIdList){
        //删除文件
        testFileManagerService.delete(getLoginUserId(), testFileIdList);
        return R.ok();
    }
}
